<template>
  <div>
    <template>
      <div>
        <!-- 面包屑导航栏 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>商品管理</el-breadcrumb-item>
          <el-breadcrumb-item>商品列表</el-breadcrumb-item>
        </el-breadcrumb>
        <el-card class="box-card">
          <!-- 表格 -->
          <div>
            <!-- 搜索框和添加按钮 -->
            <el-row :gutter="80">
              <el-col :span="8">
                <el-input placeholder="请输入查询内容" v-model="queryInfo.query" clearable>
                  <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
                </el-input>
              </el-col>
              <el-col :span="4">
                <el-button type="primary" @click="goAddPage">添加商品</el-button>
              </el-col>
            </el-row>
            <!-- 商品表格 -->
            <el-table :data="goodsList" border stripe style="margin-top: 15px; margin-bottom: 15px;">
              <el-table-column type="index" label="#" width="40px"></el-table-column>
              <el-table-column label="商品名称" prop="goods_name"></el-table-column>
              <el-table-column label="商品价格" prop="goods_price" width="80px"></el-table-column>
              <el-table-column label="商品重量" prop="goods_weight" width="80px"></el-table-column>
              <el-table-column label="创建时间" prop="add_time" width="160px">
                <template slot-scope="scope">
                  {{ scope.row.add_time | dataFormat}}
                </template>
              </el-table-column>
              <el-table-column prop="operation" label="操作" width="180px">
                <template slot-scope="scope">
                  <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditDialog(scope.row.goods_id)">编辑</el-button>
                  <el-button type="danger" @click="deleteWarning(scope.row.goods_id)" icon="el-icon-delete" size="mini">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
          </div>
          <!-- 分页 -->
          <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" background
            :current-page="queryInfo.pagenum" :page-sizes="[5, 10, 15, 20]" :page-size="queryInfo.pagesize"
            layout="total, sizes, prev, pager, next, jumper" :total="total">
          </el-pagination>
        </el-card>
      </div>
    </template>
  </div>
</template>

<script>
export default {
  created () {
    this.getGoodsList()
  },
  data () {
    return {
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      goodsList: [],
      total: 0
    }
  },
  methods: {
    showEditDialog (id) {
    },
    async getGoodsList () {
      const { data: res } = await this.$http.get('/goods', { params: this.queryInfo })
      console.log(res)
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.goodsList = res.data.goods
      this.total = res.data.total
    },
    // 监听pagesize改变
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      // 注意改变后要重新获取列表
      this.getGoodsList()
    },
    // 监听页码值改变
    handleCurrentChange (newPage) {
      this.queryInfo.pagenum = newPage
      this.getGoodsList()
    },
    // 删除商品
    async deleteWarning (id) {
      const confirmResult = await this.$confirm(
        '此操作将永久删除该商品, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(err => err)
      // 确定返回confirm 否则返回cancel
      // console.log(confirmResult)
      if (confirmResult !== 'confirm') {
        return this.$message.info('您已取消删除')
      }
      const { data: res } = await this.$http.delete(`/goods/${id}`)
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.$message.info(res.meta.msg)
      this.getGoodsList()
      if (res.data.pagenum * this.queryInfo.pagesize === res.data.total && res.data.pagenum > 1) {
        this.queryInfo.pagenum = this.queryInfo.pagenum - 1
        this.getGoodsList()
      } else if (res.data.pagenum === 1) {
        this.queryInfo.pagenum = 1
        this.getGoodsList()
      }
    },
    goAddPage () {
      this.$router.push('goods/Add')
    }
  }
}
</script>

<style lang='less' scoped>

</style>
